package com.tsd.core.openweixin.model.wxpay;


import com.tsd.core.openweixin.WechatClient;
import com.tsd.core.openweixin.utils.AdapterCDATA;
import com.tsd.core.openweixin.utils.JAXBUtils;
import com.tsd.core.openweixin.utils.PayCommonUtil;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.SortedMap;
import java.util.TreeMap;

@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class UnifiedOrderRequest {
    @XmlElement(name = "appid")
    private String appId;

    @XmlElement(name = "mch_id")
    private String mchId;

    @XmlElement(name = "device_inf")
    private String deviceInf;

    @XmlElement(name = "nonce_str")
    private String nonceStr;

    @XmlElement(name = "body")
    @XmlJavaTypeAdapter(AdapterCDATA.class)
    private String body;

    @XmlElement(name = "attach")
    @XmlJavaTypeAdapter(AdapterCDATA.class)
    private String attach;

    @XmlElement(name = "out_trade_no")
    private String outTradeNo;

    @XmlElement(name = "total_fee")
    private int totalFee;//订单总金额，单位为分，不 能带小数点

    @XmlElement(name = "spbill_create_ip")
    private String spbillCreateIp;

    @XmlElement(name = "notify_url")
    private String notifyUrl;

    @XmlElement(name = "trade_type")
    private String tradeType;//JSAPI、NATIVE、AP
//	parameters.put("trade_type", "JSAPI");

    @XmlElement(name = "openid")
    private String openId;

    @XmlElement(name = "sign")
    @XmlJavaTypeAdapter(AdapterCDATA.class)
    private String sign;//签名


    @XmlElement(name = "product_id")
    private String productId;//只在 trade_type 为 NATIVE 时需要填写。此 id 为二维码 中包含的商品 ID，商户自行 维护。

    public static String testPreOrder() {
        UnifiedOrderRequest u = new UnifiedOrderRequest();
        u.setAppId("wxc34426e16d4e2945");
        u.setMchId("1234724802");//商户号
        u.setOpenId("oEKqntzB0ttjs-cvwkqHmABfPr_M");
        u.setNonceStr(PayCommonUtil.CreateNoncestr());
        u.setBody("500元100次洗车卡");
        u.setOutTradeNo("123456");
        u.setTotalFee(10000);
        u.setSpbillCreateIp("127.0.0.1");
        u.setNotifyUrl("http://www.czbbb.cn/testwx/czb/card/testwxpayRecver.jsp?wxid=wxc34426e16d4e2945");
        u.setTradeType("JSAPI");
        u.setProductId("1234567890");
        SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
        parameters.put("appid", u.getAppId());
        parameters.put("mch_id", u.getMchId());
        parameters.put("openid", u.getOpenId());
        parameters.put("nonce_str", u.getNonceStr());
        parameters.put("body", u.getBody());
        parameters.put("out_trade_no", u.getOutTradeNo());
        parameters.put("total_fee", u.getTotalFee() + "");
        parameters.put("spbill_create_ip", u.getSpbillCreateIp());
        parameters.put("notify_url", u.getNotifyUrl());
        parameters.put("trade_type", u.getTradeType());
        parameters.put("product_id", u.getProductId());
        String paidKey = "Defger32TD33456dfgt3TEq2dt345678";
        String sign = PayCommonUtil.createSign("UTF-8", paidKey, parameters);
        u.setSign(sign);
        String responseXML = JAXBUtils.objectToXml(u, u.getClass());
        System.out.println("responseXML:\n" + responseXML);
        WechatClient wc = new WechatClient("wxc34426e16d4e2945", "e902f5cb4de1f12f4987aa5cb8fd3108", true);
        String s = wc.getPrePayId(responseXML);
        System.out.println(s);
        return s;
    }

    public static UnifiedOrderResponse getPreOrder() {
        String s = testPreOrder();
        UnifiedOrderResponse o = JAXBUtils.parserString(s, UnifiedOrderResponse.class);
        return o;
    }

    public static void main(String[] args) {
        UnifiedOrderResponse r = getPreOrder();
        System.out.println(" prepay id:" + r.getPrepayId());
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }

    public String getDeviceInf() {
        return deviceInf;
    }

    public void setDeviceInf(String deviceInf) {
        this.deviceInf = deviceInf;
    }

    public String getAttach() {
        return attach;
    }

    public void setAttach(String attach) {
        this.attach = attach;
    }

    public int getTotalFee() {
        return totalFee;
    }

    public void setTotalFee(int totalFee) {
        this.totalFee = totalFee;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getSpbillCreateIp() {
        return spbillCreateIp;
    }

    public void setSpbillCreateIp(String spbillCreateIp) {
        this.spbillCreateIp = spbillCreateIp;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getTradeType() {
        return tradeType;
    }

    public void setTradeType(String tradeType) {
        this.tradeType = tradeType;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }
}
